home *** CD-ROM | disk | FTP | other *** search
/ WINMX Assorted Textfiles / Ebooks.tar / Text - Tech - Programming - Visual Basic - Nod Programing VB Help Index 5 (TXT).zip / VBTIPS5.TXT
Text File  |  1998-07-23  |  11KB  |  306 lines

  1.  
  2.                Nod Programming Inc. VB Help Index
  3. »»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»»
  4. This is intended for free use.  The code here is for various skill levels, 
  5. anyone from beginers to advanced programers can use these.  Do what you wish 
  6. with the code it is free for you to use and manipulate!
  7.  
  8. ****************************************************************************
  9.  
  10. Use FreeFile to Prevent File Open Conflicts:
  11.  
  12. Both Access and VB let you hard code the file numbers when using the File
  13. Open statement. For example:
  14.  
  15.      Open "myfile.txt" for Append as #1
  16.      Print #1,"a line of text"
  17.      Close #1
  18.  
  19. The problem with this method of coding is that you never know which file
  20. numbers may be in use somewhere else in your program. If you attempt to use
  21. a file number already occupied, you'll get a file error. To prevent this
  22. problem, you should always use the FreeFile function. This function will
  23. return the next available file number for your use. For example:
  24.  
  25.      IntFile=FreeFile()
  26.      Open "myfile.txt" for Append as #intFile
  27.      Print #intFile,"a line of text"
  28.      Close #intFile
  29.  
  30. ****************************************************************************
  31.  
  32. Confirm Screen Resolution:
  33.  
  34. Here's a great way to stop the user from running your application in the
  35. wrong screen resolution. First, create a function called CheckRez:
  36.  
  37. Public Function CheckRez(pixelWidth As Long, pixelHeight As Long) As Boolean
  38.     '
  39.     Dim lngTwipsX As Long
  40.     Dim lngTwipsY As Long
  41.     '
  42.     ' convert pixels to twips
  43.     lngTwipsX = pixelWidth * 15
  44.     lngTwipsY = pixelHeight * 15
  45.     '
  46.     ' check against current settings
  47.     If lngTwipsX <> Screen.Width Then
  48.         CheckRez = False
  49.     Else
  50.         If lngTwipsY <> Screen.Height Then
  51.             CheckRez = False
  52.         Else
  53.             CheckRez = True
  54.         End If
  55.     End If
  56.     '
  57. End Function
  58.  
  59. Next, run the following code at the start of the program:
  60.  
  61.     If CheckRez(640, 480) = False Then
  62.         MsgBox "Incorrect screen size!"
  63.     Else
  64.         MsgBox "Screen Resolution Matches!"
  65.     End If
  66.  
  67. ****************************************************************************
  68.  
  69. Quick Text Select On GotFocus:
  70.  
  71. When working with data entry controls, the current value in the control
  72. often needs to be selected when the control received focus. This allows the
  73. user to immediately begin typing over any previous value. Here's a quick
  74. subroutine to do just that:
  75.  
  76. Public Sub FocusMe(ctlName As Control)
  77.     
  78.     With ctlName
  79.         .SelStart = 0
  80.         .SelLength = Len(ctlName)
  81.     End With
  82.     
  83. End Sub
  84.  
  85. Now add a call to this subroutine in the GotFocus event of the input
  86.  controls:
  87.  
  88. Private Sub txtFocusMe_GotFocus()
  89.     Call FocusMe(txtFocusMe)
  90. End Sub
  91.  
  92. ****************************************************************************
  93.  
  94. Use ParamArray to Accept an Arbitrary Number of Parameters:
  95.  
  96. You can use the ParamArray keyword in the declaration line of a method to
  97. create a subroutine or function that accepts an arbitrary number of
  98. parameters at runtime. For example, you can create a method that will fill
  99. a list box with some number of items even if you do not know the number of
  100. items you will be sent. Add the method below to a form:
  101.  
  102. Public Sub FillList(ListControl As ListBox, ParamArray Items())
  103.     '
  104.     Dim i As Variant
  105.     '
  106.     With ListControl
  107.         .Clear
  108.         For Each i In Items
  109.             .AddItem i
  110.         Next
  111.     End With
  112.     '
  113. End Sub
  114.  
  115. Note that the ParamArray keyword comes BEFORE the parameter in the
  116. declaration line. Now add a list box to your form and a command button. Add
  117. the code below in the "Click" event of the command button.
  118.  
  119. Private Sub Command1_Click()
  120.     '
  121.     FillList List1, "TiffanyT", "MikeS", "RochesterNY"
  122.     '
  123. End Sub
  124.     
  125. ****************************************************************************
  126.  
  127. Use FileDSNs to ease ODBC Installs:
  128.  
  129. If you're using an ODBC connection to your database, you can ease the
  130. process of installing the application on workstations by using the FileDSN
  131. (data source name) instead of the more-common UserDSN. You define your ODBC
  132. connection as you normally would with UserDSNs. However, the resulting
  133. definition is not stored in the workstation registry. Instead it gets
  134. stored in a text file with the name of the DSN followed by ".dsn" (i.e.
  135. "MyFileDSN.dsn"). The default folder for all FileDSNs is "c:\program
  136. files\common files\Odbc\data sources". Now, when you want to install the VB
  137. application that uses the FileDSN, all you need to do is add the FileDSN to
  138. the Install package and run the install as usual. No more setting up DSNs
  139. manually!
  140.  
  141. NOTE: FileDSNs are available with ODBC 3.0 and higher.
  142.  
  143. ****************************************************************************
  144.  
  145. Opening a browser to your homepage
  146.  
  147. You can use code like the following to open a browser to your homepage.
  148. Modify filenames, paths, and URLs as necessary to match the values on your
  149. system.
  150.  
  151. Dim FileName As String, Dummy As String
  152. Dim BrowserExec As String * 255
  153. Dim RetVal As Long
  154. Dim FileNumber As Integer
  155. Const SW_SHOWNORMAL =3D 1 ' Restores Window if Minimized or
  156.  
  157. Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
  158. (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
  159. ByVal lpParameters As String, ByVal lpDirectory As String, _
  160. ByVal nShowCmd As Long) As Long
  161.  
  162. Declare Function FindExecutable Lib "shell32.dll" Alias "FindExecutableA" _
  163. (ByVal lpFile As String, ByVal lpDirectory As String, ByVal lpResult As _
  164. String) As Long
  165. '<Code> ---------
  166.  
  167. BrowserExec =3D Space(255)
  168. FileName =3D "C:\temphtm.HTM"
  169.  
  170. FileNumber =3D FreeFile()     ' Get unused file number
  171.  
  172. Open FileName For Output As #FileNumber  ' Create temp HTML file
  173.   Write #FileNumber, "<HTML> <\HTML>"  ' Output text
  174. Close #FileNumber   ' Close file
  175.  
  176. ' Then find the application associated with it.
  177.   RetVal =3D FindExecutable(FileName, Dummy, BrowserExec)
  178.   BrowserExec =3D Trim$(BrowserExec)
  179.   ' If an application is found, launch it!
  180.   If RetVal <=3D 32 Or IsEmpty(BrowserExec) Then ' Error
  181.  
  182.     Msgbox "Could not find a browser"
  183.  
  184.   Else
  185.     RetVal =3D ShellExecute(frmMain.hwnd, "open", BrowserExec, _
  186.       "www.myurl.com", Dummy, SW_SHOWNORMAL)
  187.     If RetVal <=3D 32 Then        ' Error
  188.       Msgbox "Web Page not Opened"
  189.     End If
  190.   End If
  191.  
  192. Kill FileName  ' delete temp HTML file
  193.  
  194. ****************************************************************************
  195.  
  196. Creating a incrementing number box
  197.  
  198. You can't increment a vertical scroll bar's value--a fact that can become
  199. annoying. For example, start a new project and place a text box and a
  200. vertical scroll bar on the form. Place the vertical scroll bar to the right
  201. of the text box and assign their Height and Top properties the same values.
  202. Assign the vertical scroll bar a Min property value of 1 and a Max value of
  203. 10. Place the following code in the vertical scroll bar's Change event:
  204.  
  205. Text1.Text = VScroll1.Value
  206.  
  207. Now press [F5] to run the project. Notice that if you click on the bottom
  208. arrow of the vertical scroll bar, the value increases; if you click on the
  209. top arrow, the value decreases. From my perspective, it should be the other
  210. way around.
  211.  
  212. To correct this, change the values of the Max and Min properties to
  213. negative values. For example, end the program and return to the design
  214. environment. Change the vertical scroll bar's Max value to -1 and its Min
  215. value to -10. In its Change event, replace the line you entered earlier
  216. with the following:
  217.  
  218. Text1.Text = Abs(Vscroll1.Value)
  219.  
  220. Now press [F5] to run the project. When you click on the top arrow of the
  221. vertical scroll bar, the value now increases. Adjust the Height properties
  222. of the text box and the scroll bar so you can't see the position indicator,
  223. and your number box is ready to go.
  224.  
  225. ****************************************************************************
  226.  
  227. Measuring a text extent:
  228.  
  229. It's very simple to determine the extent of a string in VB. You can do so
  230. with WinAPI functions, but there's an easier way: Use the AutoSize property
  231. of a Label component. First, insert a label on a form (labMeasure) and set
  232. its AutoSize property to True and Visible property to False. Then write
  233. this simple routine:
  234.  
  235. Private Function TextExtent(txt as String) as Integer
  236.    labMeasure.Caption = txt
  237.    TextExtent = labMeasure.Width
  238. End Function
  239.  
  240. When you want to find out the extent of some text, simply call this
  241. function with the string as a parameter.
  242.  
  243. In my case it turned out that the measure was too short. I just added some
  244. blanks to the string. For example:
  245.  
  246. Private Function TextExtent(txt As String) As Integer
  247.    labMeasure.Caption = "  " & txt
  248.    TextExtent = labMeasure.Width
  249. End Function
  250.  
  251. ****************************************************************************
  252.  
  253. Importing Registry settings
  254.  
  255. You can use just a few lines of code to import Registry settings. If you
  256. have an application called myapp.exe and a Registry file called myapp.reg,
  257. the following code will put those settings into the Registry without
  258. bothering the user.
  259.  
  260. Dim strFile As String
  261. strFile =3D App.Path & "\" & opts.AppExeName & ".reg"
  262. If Len(Dir$(strFile)) > 1 Then
  263.     lngRet =3D Shell("Regedit.exe /s " & strFile, vbNormalFocus)
  264. End If
  265.  
  266. ****************************************************************************
  267.  
  268. Labeling your forms:
  269.  
  270. Do you have a ton of screens in your application? Do you also have plenty
  271. of users who want to "help you" by pointing out buttons that are one twip
  272. out of place? Sometimes it's hard to know what screen users are talking
  273. about when they're trying to communicate a problem--particularly if they're
  274. in a different location than you.
  275.  
  276. To reduce the pain of this process, I add a label (called lblHeader) to the
  277. top of each GUI window, nominally to hold start-up information for users
  278. when they first open the window. You can also use this label to hold the
  279. name of the window the user is looking at, by using the following code:
  280.  
  281. Private Sub Form_Load()
  282.     SetupScreen me
  283. End Sub
  284.  
  285.  
  286. Public SetupScreen (frm as Form)
  287.     ' Do other set-up stuff here (fonts, colors).
  288.     HookInFormName frm
  289. End Sub
  290.  
  291. Public Sub HookInFormName(frm As Form)
  292.     ' The Resume Next on Error allows forms that do not use a standard
  293.     ' header label to get past this.
  294.     On Error Resume Next
  295.     frm.lblHeader.Caption = "(" & frm.Name & ") " & frm.lblHeader.Caption
  296. End Sub
  297.  
  298. Note that if you don't want to use a label, that you can also use code like
  299.  
  300.     frm.print frm.name
  301.  
  302. to print to the back of the window itself.
  303.  
  304. ****************************************************************************
  305.  
  306.                        End of Help 5 of how many I do!!